﻿namespace CompanyPayRollSystem
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using CompanyPayRollSystem.BusinessLayer;
    using CompanyPayRollSystem.Database;

    /// <summary>
    /// Form used to set salary items
    /// </summary>
    public partial class SetSalaryItemsForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Windows.Forms.Form"/> class.
        /// </summary>
        public SetSalaryItemsForm()
        {
            InitializeComponent();
            this.LoadDepartments();
            this.LoadMonths();
            this.LoadYears();
            this.LoadEmployees();
        }

        /// <summary>
        /// Handles the CheckedChanged event of the rbSpecificEmployee control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void rbSpecificEmployee_CheckedChanged(object sender, EventArgs e)
        {
            this.cmbEmployees.Visible = this.rbSpecificEmployee.Checked;
            this.cmbDepartments.Visible =  !this.rbSpecificEmployee.Checked;
        }
 

        /// <summary>
        /// Handles the CheckedChanged event of the rbSpecificDepartment control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void rbSpecificDepartment_CheckedChanged(object sender, EventArgs e)
        {
            this.cmbDepartments.Visible = this.rbSpecificDepartment.Checked;
            this.cmbEmployees.Visible =!this.rbSpecificDepartment.Checked;
        }

        /// <summary>
        /// Handles the CheckedChanged event of the rbAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void rbAll_CheckedChanged(object sender, EventArgs e)
        {
            this.cmbDepartments.Visible =
                this.cmbEmployees.Visible =!this.rbAll.Checked;
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            double bonusPercentage;
            int month = this.cmbMonth.SelectedIndex + 1;
            int year = (int)this.cmbYear.SelectedItem;
            if (!double.TryParse(this.txtBonus.Text, out bonusPercentage))
            {
                MessageBox.Show("Invalid bonus format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            double incentive ;
            bool sucess = double.TryParse(this.txtIncentive.Text, out incentive);
            if(!sucess)
            {
                MessageBox.Show("Invalid incentive format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.SetSalaryItems(bonusPercentage, month, year,incentive);
        }

        #region Load Comboboxes

        /// <summary>
        /// Loads the departments.
        /// </summary>
        private void LoadDepartments()
        {
            List<Department> departments = DepartmentManager.GetDepartments();
            this.cmbDepartments.DisplayMember = ColumnsNames.DepartmentColumnsNames.DepartmentName;
            this.cmbDepartments.ValueMember = ColumnsNames.DepartmentColumnsNames.DepartmentID;
            this.cmbDepartments.DataSource = departments;
        }

        
        /// <summary>
        /// Loads the employees.
        /// </summary>
        private void LoadEmployees()
        {
            List<Employee> employees = EmployeeManager.GetEmployees();
            this.cmbEmployees.DataSource = employees;
            this.cmbEmployees.DisplayMember = ColumnsNames.EmployeeColumnsNames.EmployeeName;
            this.cmbEmployees.ValueMember = ColumnsNames.EmployeeColumnsNames.EmployeeID;
        }

        /// <summary>
        /// Loads the months.
        /// </summary>
        private void LoadMonths()
        {
            string[] months = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
            for (int i = 0; i < 12; i++)
            {
                this.cmbMonth.Items.Add(months[i]);
            }

            this.cmbMonth.SelectedIndex = DateTime.Now.Month - 1;
        }

        /// <summary>
        /// Loads the years.
        /// </summary>
        private void LoadYears()
        {
            for (int i = DateTime.Now.Year - 3; i <= DateTime.Now.Year; i++)
            {
                this.cmbYear.Items.Add(i);
            }

            this.cmbYear.SelectedIndex = this.cmbYear.Items.Count - 1;
        }
        #endregion
        #region Business
        /// <summary>
        /// Sets the salary items.
        /// </summary>
        /// <param name="bonusPercentage">The bonus percentage.</param>
        /// <param name="incentivesPercentage">The incentives percentage.</param>
        /// <param name="month">The month.</param>
        /// <param name="year">The year.</param>
        private void SetSalaryItems(double bonusPercentage, int month, int year, double incentive)
        {
            try
            {
                if (this.rbAll.Checked)
                {
                    EmployeeSalaryItemsManager.SaveAllEmployeesSalaryItems(month, year, bonusPercentage,incentive);
                }

                if (this.rbSpecificDepartment.Checked)
                {
                    int departmentID;
                    if (this.cmbDepartments.SelectedIndex > -1)
                    {
                        departmentID = ((Department)this.cmbDepartments.SelectedItem).DepartmentID;
                    }
                    else
                    {
                        MessageBox.Show("Specify department.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    EmployeeSalaryItemsManager.SaveDepartmentSalaryItems(departmentID, month, year, bonusPercentage, incentive);
                }

                if (this.rbSpecificEmployee.Checked)
                {
                    int employeeID;
                    if (this.cmbEmployees.SelectedValue == null)
                    {
                        MessageBox.Show("Specify employee.");
                        return;
                    }
                    else
                    {
                        employeeID = (int)this.cmbEmployees.SelectedValue;
                    }

                    EmployeeSalaryItemsManager.SaveEmployeeSalaryItems(employeeID, month, year, bonusPercentage, incentive);
                }

                MessageBox.Show("Salary Items saved successfully.", "Added", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Salary Items not saved.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
    }
}
